use nalgebra::Point2;

// 坦克基础属性
#[derive(Debug, Clone, Copy)]
pub struct Tank {
    pub position: Point2<f32>,
    pub health: f32,           // 生命值
    pub speed: f32,            // 移动速度
    pub attack_range: f32,     // 攻击范围
    pub attack_damage: f32,    // 攻击伤害
    pub attack_cooldown: f32,  // 攻击冷却时间
    pub current_cooldown: f32, // 当前冷却时间
    pub id: usize,             // 坦克ID
    pub team: usize,           // 队伍ID
    pub is_invincible: bool,   // 是否无敌
    pub invincible_timer: f32, // 无敌计时器
}

impl Tank {
    pub fn new(position: Point2<f32>, id: usize, team: usize) -> Self {
        Tank {
            position,
            health: 100.0,
            speed: 3.0,
            attack_range: 250.0,
            attack_damage: 30.0,
            attack_cooldown: 45.0,
            current_cooldown: 0.0,
            id,
            team,
            is_invincible: false,
            invincible_timer: 0.0,
        }
    }
    
    // 更新坦克状态
    pub fn update(&mut self, dt: f32) {
        // 更新冷却时间
        if self.current_cooldown > 0.0 {
            self.current_cooldown -= dt;
        }
        
        // 更新无敌状态
        if self.is_invincible {
            self.invincible_timer -= dt;
            if self.invincible_timer <= 0.0 {
                self.is_invincible = false;
            }
        }
    }
    
    // 检查是否可以攻击
    pub fn can_attack(&self) -> bool {
        self.current_cooldown <= 0.0 && self.health > 0.0
    }
    
    // 执行攻击
    pub fn attack(&mut self) -> bool {
        if self.can_attack() {
            self.current_cooldown = self.attack_cooldown;
            true
        } else {
            false
        }
    }
    
    // 受到伤害
    pub fn take_damage(&mut self, damage: f32) {
        if !self.is_invincible && self.health > 0.0 {
            self.health -= damage;
            if self.health <= 0.0 {
                self.health = 0.0;
            }
        }
    }
    
    // 治疗
    pub fn heal(&mut self, amount: f32) {
        self.health += amount;
        if self.health > 100.0 {
            self.health = 100.0;
        }
    }
    
    // 应用道具效果
    pub fn apply_prop(&mut self, prop_type: crate::game_prop::GamePropType, value: f32, duration: f32) {
        match prop_type {
            crate::game_prop::GamePropType::Health => {
                self.heal(value);
            },
            crate::game_prop::GamePropType::Invincible => {
                self.is_invincible = true;
                self.invincible_timer = duration;
            },
            crate::game_prop::GamePropType::Range => {
                self.attack_range *= value;
            },
            crate::game_prop::GamePropType::Speed => {
                self.speed *= value;
            },
            crate::game_prop::GamePropType::Damage => {
                self.attack_damage *= value;
            },
        }
    }
    
    // 检查是否存活
    pub fn is_alive(&self) -> bool {
        self.health > 0.0
    }
}